package com.gph.saviorframework.security.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gph.saviorframework.Constants;
import com.gph.saviorframework.FieldType;
import com.gph.saviorframework.common.model.ExpressionType;
import com.gph.saviorframework.common.model.ResourcePermission;
import com.gph.saviorframework.dao.security.ResourcePermissionDao;
import com.gph.saviorframework.security.service.ResourcePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 数据权限服务实现
 */
public class ResourcePermissionServiceImpl implements ResourcePermissionService{

    @Autowired
    private ResourcePermissionDao dao;

    private Map<String,String> fields = new HashMap<String,String>();

    private ResourcePermissionServiceImpl(){
        fields.put("ID",FieldType.STRING);
        fields.put("USER_ID",FieldType.STRING);
        fields.put("RESOURCE_TYPE",FieldType.STRING);
        fields.put("PERMISSION_TYPE",FieldType.STRING);
    }

    @Transactional
    @Override
    public void save(ResourcePermission resourcePermission) {
        dao.save(resourcePermission);
    }

    @Transactional(readOnly = true)
    @Override
    public ResourcePermission get(String id) {
        return dao.get(id);
    }

    @Transactional(readOnly = true)
    @Override
    public List<ResourcePermission> getByUserId(String userId) {
        return dao.getByUserId(userId);
    }

    /**
     * 根据用户编号和资源类型查找数据权限
     *
     * @param userId
     * @param resourceType
     * @return
     */
    @Override
    public ResourcePermission getByUserIdAndResourceType(String userId, String resourceType) {
        return dao.getByUserIdAndResourceType(userId,resourceType);
    }

    @Override
    public void update(ResourcePermission resourcePermission) {

    }

    @Override
    public void update(Set<ResourcePermission> resourcePermissions) {

    }

    @Override
    public List<Map<String, Object>> find(String sort, String dir, Map<String, Object> params) {
        return dao.find(sort,dir,genWhereClause(params));
    }

    @Override
    public Map<String, Object> find(int start, int limit, String sort, String dir, Map<String, Object> params, Object... extras) {
        Map<String,Object> map = new HashMap<String, Object>();

        PageHelper.startPage(start, limit);
        List<Map<String,Object>> list = find(sort, dir, params);
        PageInfo pageInfo = new PageInfo(list);

        map.put(Constants.DEFAULT_RECORD_MODEL_KEY, list);
        map.put(Constants.DEFAULT_COUNT_MODEL_KEY, pageInfo.getPages());
        map.put(Constants.DEFAULT_RECORDS_MODEL_KEY,pageInfo.getTotal());
        map.put(Constants.DEFAULT_SUCCESS_KEY, Boolean.TRUE);

        return map;
    }

    @Override
    public long count(Map<String, Object> params) {
        return 0;
    }

    @Override
    public void delete(String id) {
        dao.deleteById(id);
    }

    @Transactional
    @Override
    public void deleteByUserId(String userId) {
        dao.deleteByUserId(userId);
    }

    @Transactional
    @Override
    public void deleteByResourceType(String resourceType) {
        dao.deleteByResourceType(resourceType);
    }

    @Transactional
    @Override
    public void deleteByResourceTypeAndUserId(String userId, String resourceType) {
        dao.deleteByResourceTypeAndUserId(userId,resourceType);
    }

    /**
     * 判断是否有某种权限
     *
     * @param permission
     * @param expressionType
     * @return
     */
    @Override
    public boolean hasPermission(ResourcePermission permission, ExpressionType expressionType) {
        String expression = permission.getExpression();
        if(null!=expression&&expression.contains(expressionType.name())){
            return true;
        }
        return false;
    }

    @Override
    public void deleteByIds(String[] ids) {
        //todo:根据ids批量删除
        dao.batchDelete(ids);
    }

    @Override
    public Map<String, String> getFields() {
        return fields;
    }

    @Override
    public Map<String, Object> findById(String id) {
        return null;
    }

    @Override
    public String genWhereClause(Map<String, Object> params, Object... extra) {
        String whereClause = "1=1";
        if (params!=null && params.keySet().size() > 0) {
            for (String key : params.keySet()) {
                if (fields.get(key).equals(FieldType.STRING)) {
                    whereClause += " AND T." + key + " LIKE '"+params.get(key)+"'";
                } else {
                    whereClause += " AND T." + key + " ='"+params.get(key)+"'";
                }
            }
        }
        return whereClause;
    }
}
